package com.xydtech.ecard.busi.service.impl;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiEcardBusiReservation;
import com.xydtech.ecard.busi.mapper.EcardApiEcardBusiReservationMapper;
import com.xydtech.ecard.busi.service.IEcardApiEcardBusiReservationService;
import com.xydtech.ecard.config.mapper.EcardGridManageMapper;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.mapper.EcardApiUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务预约登记Service业务层处理
 *
 * @author Daniel
 * @date 2021-09-06
 */
@Service
public class EcardApiEcardBusiReservationServiceImpl implements IEcardApiEcardBusiReservationService {
    @Autowired
    private EcardApiEcardBusiReservationMapper ecardApiEcardBusiReservationMapper;
    @Autowired
    private EcardApiUserMapper userMapper;
    @Autowired
    private EcardGridManageMapper ecardGridManageMapper;

    /**
     * 电子名片-业务预约登记-查询已经提交过的未处理的产品预约
     *
     * @param busiReservation
     * @return
     */
    @Override
    public List<EcardApiEcardBusiReservation> getSubmitBusiReservation(EcardApiEcardBusiReservation busiReservation, String salt) {
        List<EcardApiEcardBusiReservation> busiReservationList = ecardApiEcardBusiReservationMapper.getSubmitBusiReservation(busiReservation);
        if (busiReservationList.size() > 0) {
            for (EcardApiEcardBusiReservation reservation : busiReservationList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(reservation.getTelNo())) {
                        reservation.setTelNo(AESEncryptionUtils.decrypt(reservation.getTelNo(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(reservation.getCardNum())) {
                        reservation.setCardNum(AESEncryptionUtils.decrypt(reservation.getCardNum(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReservationList;
    }

    /**
     * 电子名片-业务预约登记-预约提交
     *
     * @param busiReservation
     * @return
     */
    @Override
    public int submitReservation(EcardApiEcardBusiReservation busiReservation, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(busiReservation.getTelNo())) {
                busiReservation.setTelNo(AESEncryptionUtils.encrypt(busiReservation.getTelNo(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(busiReservation.getCardNum())) {
                busiReservation.setCardNum(AESEncryptionUtils.encrypt(busiReservation.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardApiEcardBusiReservationMapper.submitReservation(busiReservation);
    }

    /**
     * 电子名片-业务预约登记-获取客户已经预约过的第一条业务
     *
     * @param currentUserId
     * @return
     */
    @Override
    public List<EcardApiEcardBusiReservation> getCustFirstReservation(String currentUserId, Long deptId, String salt) {
        List<EcardApiEcardBusiReservation> busiReservationList = ecardApiEcardBusiReservationMapper.getCustFirstReservation(currentUserId, deptId);
        if (busiReservationList.size() > 0) {
            for (EcardApiEcardBusiReservation busiReservation : busiReservationList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getTelNo())) {
                        busiReservation.setTelNo(AESEncryptionUtils.decrypt(busiReservation.getTelNo(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getCardNum())) {
                        busiReservation.setCardNum(AESEncryptionUtils.decrypt(busiReservation.getCardNum(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getCmPhoneNo())) {
                        busiReservation.setCmPhoneNo(AESEncryptionUtils.decrypt(busiReservation.getCmPhoneNo(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReservationList;
    }

    /**
     * 电子名片-业务预约登记-获取客户已经预约过的业务
     *
     * @param currentUserId
     * @return
     */
    @Override
    public List<EcardApiEcardBusiReservation> getCustReservationList(String currentUserId, Long deptId, String salt) {
        List<EcardApiEcardBusiReservation> busiReservationList = ecardApiEcardBusiReservationMapper.getCustReservationList(currentUserId, deptId);
        if (busiReservationList.size() > 0) {
            for (EcardApiEcardBusiReservation busiReservation : busiReservationList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getTelNo())) {
                        busiReservation.setTelNo(AESEncryptionUtils.decrypt(busiReservation.getTelNo(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getCardNum())) {
                        busiReservation.setCardNum(AESEncryptionUtils.decrypt(busiReservation.getCardNum(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getCmPhoneNo())) {
                        busiReservation.setCmPhoneNo(AESEncryptionUtils.decrypt(busiReservation.getCmPhoneNo(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReservationList;
    }

    /**
     * 电子名片-业务预约登记-获取客户经理的业务预约列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<EcardApiEcardBusiReservation> getReservationForManager(String userId, String sts, String salt) {
        List<EcardApiEcardBusiReservation> busiReservationList = ecardApiEcardBusiReservationMapper.getReservationForManager(userId, sts);
        if (busiReservationList.size() > 0) {
            for (EcardApiEcardBusiReservation busiReservation : busiReservationList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getTelNo())) {
                        busiReservation.setTelNo(AESEncryptionUtils.decrypt(busiReservation.getTelNo(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getCardNum())) {
                        busiReservation.setCardNum(AESEncryptionUtils.decrypt(busiReservation.getCardNum(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReservationList;
    }

    /**
     * 电子名片-首页-根据擅长业务查询对应的客户经理
     *
     * @param adeptPrdType
     * @return
     */
    @Override
    public List<EcardApiEcardRegistInfo> getCustomerManagerByAdeptPrdType(String adeptPrdType, List<SysDept> deptList, Long parentDeptId) {
        return ecardApiEcardBusiReservationMapper.getCustomerManagerByAdeptPrdType(adeptPrdType, deptList, parentDeptId);
    }

    /**
     * 根据ID查询预约详情
     *
     * @param resId
     * @return
     */
    @Override
    public EcardApiEcardBusiReservation getReservationDetail(String resId, String salt) {
        List<EcardApiEcardBusiReservation> ecardBusiReservations = ecardApiEcardBusiReservationMapper.getReservationDetail(resId);
        if (ecardBusiReservations.size() > 0) {
            for (EcardApiEcardBusiReservation busiReservation : ecardBusiReservations) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getTelNo())) {
                        busiReservation.setTelNo(AESEncryptionUtils.decrypt(busiReservation.getTelNo(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservation.getCardNum())) {
                        busiReservation.setCardNum(AESEncryptionUtils.decrypt(busiReservation.getCardNum(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return ecardBusiReservations.size() > 0 ? ecardBusiReservations.get(0) : null;
    }

    /**
     * 电子名片-业务预约登记-对预约业务无意向
     *
     * @param resId
     * @return
     */
    @Override
    public int noIntentionForReservation(String resId) {
        return ecardApiEcardBusiReservationMapper.noIntentionForReservation(resId);
    }

    /**
     * 电子名片-业务预约登记-受理确认
     *
     * @param ecardBusiReservation
     * @return
     */
    @Override
    public int acceptReservation(EcardApiEcardBusiReservation ecardBusiReservation) {
        return ecardApiEcardBusiReservationMapper.acceptReservation(ecardBusiReservation);
    }

    /**
     * 电子名片-业务预约登记-上传业务办理凭证（更新文件路径到预约业务中）
     *
     * @param resId
     * @param certImageFilePath
     * @return
     */
    @Override
    public int updateReservationCertImage(String resId, String certImageFilePath) {
        return ecardApiEcardBusiReservationMapper.updateReservationCertImage(resId, certImageFilePath);
    }

    /**
     * 电子名片-业务预约登记-业务转交
     *
     * @param resId
     * @param currentUserId
     * @param transToRegistId
     * @return
     */
    @Override
    public int transReservation(String resId, String currentUserId, String transToRegistId) {
        return ecardApiEcardBusiReservationMapper.transReservation(resId, currentUserId, transToRegistId);
    }

    /**
     * 更新业务预约的报告地址
     *
     * @param resId
     * @param reportUrl
     * @return
     */
    @Override
    public int updateReportUrl(String resId, String reportUrl) {
        return ecardApiEcardBusiReservationMapper.updateReportUrl(resId, reportUrl);
    }

    /**
     * 修改业务预约登记
     *
     * @param ecardApiEcardBusiReservation 业务预约登记
     * @return 结果
     */
    @Override
    public int updateEcardApiEcardBusiReservation(EcardApiEcardBusiReservation ecardApiEcardBusiReservation, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardApiEcardBusiReservation.getTelNo())) {
                ecardApiEcardBusiReservation.setTelNo(AESEncryptionUtils.encrypt(ecardApiEcardBusiReservation.getTelNo(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardApiEcardBusiReservation.getCardNum())) {
                ecardApiEcardBusiReservation.setCardNum(AESEncryptionUtils.encrypt(ecardApiEcardBusiReservation.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardApiEcardBusiReservationMapper.updateEcardApiEcardBusiReservation(ecardApiEcardBusiReservation);
    }

    @Override
    public int updatePdfReport(String pdfKey, String pdfVal, String resId) {
        return ecardApiEcardBusiReservationMapper.updatePdfReport(pdfKey,pdfVal,resId);
    }
}